package com.gzhryc.shared_device.oem.h5.mng.controllers.place;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceBusinessConfig;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;

public class PlaceBusinessConfigController {

    @Route(value = "/place/business_config/list.data")
    public ListResult<PlaceBusinessConfig> listData(HttpServletRequest request) {
        ListResult<PlaceBusinessConfig> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        Long placeId = WebUtils.getLong(request,"placeId");
        List<PlaceBusinessConfig> businessConfigList = PlaceBusinessConfigService.self().findByPlaceId(placeId);
        if(businessConfigList != null && businessConfigList.size() > 0){
            result.setCode(DtoConstants.SUCCESS, null);
            result.getBody().setData(businessConfigList);
        }
        return result;
    }

    @Route(value = {"/place/business_config/add.html"})
    public String add(HttpServletRequest request) {
        Long placeId = WebUtils.getLong(request, "placeId");
        PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
        request.setAttribute("place", place);
        request.setAttribute("businessTypeList",EBusinessType.findBusinessTypes());
        return "/place/business_config/edit.html";
    }

    @Route(value = {"/place/business_config/add.do"})
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增失败，请稍后再试");
        Long placeId = WebUtils.getLong(request, "placeId");
        String businessType = WebUtils.getString(request,"businessType");
        if(placeId != null && StringTools.isNotBlank(businessType)){
            PlaceBusinessConfig businessConfig = new PlaceBusinessConfig();
            businessConfig.setPlaceId(placeId);
            businessConfig.setBusinessType(businessType);
            try {
                if(PlaceBusinessConfigService.self().add(businessConfig)){
                    result.setCode(DtoConstants.SUCCESS,null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
    
    @Route(value = {"/place/business_config/device_count/refresh.do"})
    public AjaxResult doRefresh(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "刷新失败，请稍后再试");
        Long placeId = WebUtils.getLong(request,"placeId");
        String businessType = WebUtils.getString(request,"businessType");
        if(placeId != null && StringTools.isNotBlank(businessType)) {
        	if (PlaceBusinessConfigService.self().refreshDeviceCount(placeId,businessType) > -1) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        }
        return result;
    }


    @Route(value = {"/place/business_config/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败，请稍后再试");
        Long placeId = WebUtils.getLong(request,"placeId");
        String businessType = WebUtils.getString(request,"businessType");
        if(placeId != null && StringTools.isNotBlank(businessType)) {
            try {
                if (PlaceBusinessConfigService.self().deleteByKeys(placeId,businessType)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = {"/place/business_config/cost.html"})
    public void funcCost(HttpServletRequest request, HttpServletResponse response) {
        Long placeId = WebUtils.getLong(request,"placeId");
        String businessType = WebUtils.getString(request,"businessType");
        if(EBusinessType.CDCZ.index().equals(businessType)){
            WebUtils.sendRedirect(request,response,"/cdcz/place/cost.html?placeId="+placeId);
        }else if(EBusinessType.CDB.index().equals(businessType)){
            WebUtils.sendRedirect(request,response,"/cdb/place/cost.html?placeId="+placeId);
        }else if(EBusinessType.BXSCN.index().equals(businessType)){
            WebUtils.sendRedirect(request,response,"/bxscn/place/cost.html?placeId="+placeId);
        }else if(EBusinessType.BXSCN_SHOP.index().equals(businessType)){
            WebUtils.sendRedirect(request,response,"/bxscn/place/shop_cost.html?placeId="+placeId);
        }else if(EBusinessType.PCL.index().equals(businessType)){
            WebUtils.sendRedirect(request,response,"/pcl/place/cost.html?placeId="+placeId);
        }
    }

}
